<?php

namespace App\Models;


use App\Service\Base\Model;
use App\Service\Ip;
use Illuminate\Support\Facades\DB;

class NewsComment extends Model
{

    // 用户新鲜事：收藏
    protected $table = 'news_comment';

    // 缓存字段，用于搜索绑定的字段
    protected $cacheFields = ['id', 'uid', 'news_id',];

    // {"id":"","news_id":"文章ID","user_id":"用户ID","pid":"父级评论id","author":"作者","author_ip":"作者ip","content":"内容","comment_karma":"","comment_approved":"","comment_type":"","cdate":"发布时间","mdate":"修改时间"}


    //[ 'id', 'news_id', 'user_id', 'pid', 'author', 'author_ip', 'content', 'comment_karma', 'comment_approved', 'comment_type', 'cdate', 'mdate',]


    // 评论
    // 评论, $pid = 0
    public function onComment($uid, $news_id, $content, $pid = 0, $terminal = '')
    {
        if(empty($uid)){
            return $this->return_false("请指定评论的用户!");
        }
        if(empty($news_id)){
            return $this->return_false("请指定评论的内容编号!");
        }

        $userDb = new User();
        $model  = new News();
        $where  = ["id" => $news_id,];
        $info   = $model->isCache(true)->oneFind($where);
        if(empty($info)){
            return $this->return_false("news_has_no_exist");
        }
        if(isset($info['is_draft']) && $info['is_draft'] == 1){
            return $this->return_false("news_has_not_been_reviewed");
        }
        if($info['verify'] == 0){
            return $this->return_false("news_has_not_been_reviewed");
        }
        if($info['verify'] == -1){
            return $this->return_false("news_has_been_deleted");
        }


        // 保存评论
        $ip      = request()->ip();
        $addon   = ["comment_no" => DB::raw("comment_no+1")];
        $comment = ['uid' => $uid, "news_id" => $news_id, 'content' => $content, 'ip_address' => $ip, 'ip' => (new Ip())->getIpAddress($ip), 'pid' => $pid,'verify' => 1]; // 'pid' => $pid,
        $this->fill($comment);


        // 用户发布数量
        //$userData = ["publish_no" => DB::raw('publish_no+1'), 'mtime' => LARAVEL_TIME];

        // 执行操作
        DB::beginTransaction();
        try{
            // 保存评论
            $result = $this->save();

            // 更新父级
            /*if($pid){
                $this->where("id", $pid)->update($upComment);
            }*/

            // 评论数量
            //$model->where('id', $news_id)->update($addon);

            // 如果是评论 则增加用户发布数量用于热门用户搜索
            //$userDb->where('id', $uid)->update($userData);

            DB::commit();
        } catch(\Exception $e){
            DB::rollBack();

            return $this->return_false($e->getMessage());
        }

        // 缓存操作
        $this->deleteCacheShow($comment);
        //$model->deleteCacheShow(['id' => $news_id]); // 文章或者新闻的缓存（列表数量也要编号）
        //$userDb->deleteCacheShow(['id' => $uid]);

        return $this->id;
    }

    // 定义验证操作
    public function rules()
    {

        $add = [
        ];

        $edit = [
        ];

        $info = [
        ];


        $rule = [
            'uid'     => 'required',
            'news_id' => 'numeric',
            //'en_content' => 'required',
            //'tc_content' => 'required',
            //'cn_content' => 'required',
            //'ip'      => 'required',
        ];


        $msg = [
            'id'               => '',
            'news_id'          => '文章ID',
            'uid'              => '用户ID',
            'pid'              => '父级评论id',
            'author'           => '作者',
            'author_ip'        => '作者ip',
            'en_content'       => '内容（英文）',
            'cn_content'       => '内容（中文）',
            'tc_content'       => '内容（繁体）',
            'comment_karma'    => '',
            'comment_approved' => '',
            'comment_type'     => '',
            'ctime'            => '发布时间',
            'mtime'            => '修改时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add];
    }
}
